package cc.news

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.DataBindingUtil
import androidx.databinding.Observable
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cc.NewsModel
import cc.bean.NewsInfo
import cc.common.base.BaseObserver
import cc.news.databinding.FragmentNewsBinding
import cc.news.databinding.ListItemNewsBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.trello.rxlifecycle3.components.support.RxFragment
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.fragment_news.*
import androidx.recyclerview.widget.SimpleItemAnimator
import cc.bean.ReadTipBean


/**
 * 新闻列表界面。
 */
class NewsFragment : RxFragment() {

    lateinit var fragmentNewsBinding: FragmentNewsBinding
    private val readTipBean = ReadTipBean()
    private val adapter by lazy {
        object : BaseQuickAdapter<NewsInfo, BaseViewHolder>(R.layout.list_item_news) {
            override fun convert(helper: BaseViewHolder, item: NewsInfo?) {
                DataBindingUtil.getBinding<ListItemNewsBinding>(helper.itemView)?.setVariable(BR.news, item)
            }

            override fun getItemView(layoutResId: Int, parent: ViewGroup?): View {
                val binding = DataBindingUtil.inflate<ListItemNewsBinding>(LayoutInflater.from(context), R.layout.list_item_news, parent, false)
                return binding.root
            }
        }.also {
            it.setOnItemClickListener { adapter, view, position ->
                readTipBean.readCount++
                readTipBean.tip = "阅读数${readTipBean.readCount}"
                it.getItem(position)?.isRead = true
                it.notifyItemChanged(position)
                Toast.makeText(context, "你点击了$position", Toast.LENGTH_SHORT).show()
            }
        }
    }

    private val newsModel by lazy {
        NewsModel()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return DataBindingUtil.inflate<FragmentNewsBinding>(inflater, R.layout.fragment_news, container, false).also {
            this.fragmentNewsBinding = it
        }.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        DataBindingUtil.getBinding<FragmentNewsBinding>(view)?.run {
            (rvNews.getItemAnimator() as SimpleItemAnimator).supportsChangeAnimations = false
            rvNews?.layoutManager = LinearLayoutManager(context) as RecyclerView.LayoutManager?
            rvNews?.adapter = adapter
            newsModel.news.compose(bindToLifecycle()).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
                    .subscribe(object : BaseObserver<List<NewsInfo>>(this@NewsFragment) {
                        override fun doNext(t: List<NewsInfo>?) {
                            adapter.setNewData(t)
                        }
                    })
        }
        //改变属性
        fragmentNewsBinding.readTip = readTipBean
        readTipBean.tip="未开始阅读"
    }
}